#include "loginmainwindow.h"
#include "ui_loginmainwindow.h"
#include <QSqlError>
#include "singupmainwindow.h"
#include "onlinemusicwidget.h"

LoginMainWindow::LoginMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginMainWindow)
{
    ui->setupUi(this);
    // 去掉窗口标题栏
    this->setWindowFlag(Qt::FramelessWindowHint);

    // 禁止窗口改变尺寸大小
    this->setFixedSize(this->width(),this->height());
    //光标定位
    ui->textEdit_Input->setFocus();
    /*QPushButton *button = new QPushButton("Hide to Tray", this);
            connect(button, &QPushButton::clicked, this, &LoginMainWindow::hideToTray);

            trayIcon = new QSystemTrayIcon(this);
            connect(trayIcon, &QSystemTrayIcon::activated, this, &LoginMainWindow::iconActivated);

            QMenu *trayIconMenu = new QMenu(this);
            QAction *restoreAction = new QAction("Restore", trayIconMenu);
            connect(restoreAction, &QAction::triggered, this, &LoginMainWindow::showNormal);
            trayIconMenu->addAction(restoreAction);
            trayIcon->setContextMenu(trayIconMenu);

            setCentralWidget(button);
            */


}

LoginMainWindow::~LoginMainWindow()
{
    delete ui;
}

void LoginMainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //更改背景图片
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/new/prefix1/images/Login_background.jpg"));
}
/*
void LoginMainWindow::closeEvent(QCloseEvent *event)
{
     if (trayIcon->isVisible()) {
         hide();
         event->ignore();
     }
}

void LoginMainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
                case QSystemTrayIcon::DoubleClick:
                    showNormal();
                    break;
                default:
                    break;
    }
}

void LoginMainWindow::showNormal()
{
    show();
    trayIcon->hide();
    setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
    raise();
    activateWindow();
}

void LoginMainWindow::hideToTray()
{
    hide();
    trayIcon->show();
}
*/

void LoginMainWindow::on_pushButton_3_clicked()
{
    this->hide();
}

void LoginMainWindow::on_pushButton_4_clicked()
{
    this->close();
}

void LoginMainWindow::on_pushButton_Login_clicked()
{
    sql_Init(); //数据库初始化
    QString username = ui->textEdit_Input->toPlainText();
    QString password = ui->textEdit_Password->toPlainText();
    QSqlQuery query;
    query.prepare("SELECT * FROM infor WHERE username = :username AND password = :password");
    query.bindValue(":username", username);
    query.bindValue(":password", password);
        //query.prepare("SELECT id FROM infor WHERE username=username");
        //query.bindValue(":num",num);

    if (!query.exec()) {
         qDebug() << "Query execution error:" << query.lastError().text();
         QMessageBox::information(this, "登录认证", "登录失败, 查询执行错误：" + query.lastError().text());
         return;
        }

        if (query.next()) {
            //Currentname = username; // 假设这是全局变量或成员变量
            qDebug() << "Login success";
            QMessageBox::information(this, "登录认证", "登录成功");
            onlineMusicWidget *w = new onlineMusicWidget;  //不用使用this
            w->show();
            this->close(); // 关闭登录界面
        } else {
            qDebug() << "Login error: Username or password incorrect";
            QMessageBox::information(this, "登录认证", "登录失败, 账户或者密码错误");
        }
}

void LoginMainWindow::sql_Init()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName("localhost"); // 设置MySQL服务器地址
        db.setDatabaseName("account_information"); // 设置数据库名
        db.setUserName("root"); // 设置数据库用户名
        db.setPassword("083911Aa"); // 设置数据库密码

        if (!db.open()) {
            qDebug() << "Database connection error";
            return; // 如果无法打开数据库连接，则退出函数
        }

        // 创建表
        QString createsql = QString("CREATE TABLE IF NOT EXISTS infor ("
                                 "id INT AUTO_INCREMENT PRIMARY KEY,"
                                 "username VARCHAR(20) UNIQUE NOT NULL,"
                                 "password VARCHAR(20) NOT NULL)");

        QSqlQuery query;
        if (!query.exec(createsql)) {
            qDebug() << "Table create error:" << query.lastError().text();
        } else {
            qDebug() << "Table create success";
        }
}

void LoginMainWindow::on_pushButton_singUp_clicked()
{
    //跳转
    SingUpMainWindow *singupWindow = new SingUpMainWindow(this);
    singupWindow->show();
}
